package com.ssm.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ssm.pojo.Category;
import com.ssm.util.Page;
 


//告诉spring mvc这是一个控制器类
@Controller
public class CategoryController extends BaseController {
	
	  // 类别管理首页
	  @RequestMapping("/")
	  public String index(){
		  return "index";
	  }
	 
	 // 类别列表
	 @RequestMapping(value="/category",method = RequestMethod.GET)
	 public String listCategory(ModelMap model,Page page){
		 list(model, page);
	     return "listCategory";
	 }
	 
	// 前往添加类别
	@RequestMapping(value="/category/add",method=RequestMethod.GET)
	public String add(){
		return "addCategory";
	}
	// 添加类别
	@RequestMapping(value="/category",method=RequestMethod.POST)
	public String add(ModelMap model,Category category) {
		Integer result = categoryService.add(category);
		if (result.equals(1)) return "redirect:/category?start="+nowStart(categoryService.total());
	    model.addAttribute("error", "添加类别出错");
		return "error";
	}
	// 删除类别
	@RequestMapping(value="/category/{id}",method=RequestMethod.DELETE)
	public String delete(ModelMap model,Category category) {
		Integer result = categoryService.delete(category.getId());
		if (result.equals(1)) return "redirect:/category?start="+nowStart(categoryService.total(category.getId()));
	    model.addAttribute("error", "删除类别出错");
		return "error";
	}
	// 前往修改类别
	@RequestMapping(value="/category/{id}",method=RequestMethod.GET)
	public String edit(ModelMap model,Category category){
		category = categoryService.get(category.getId());
		if(category != null) {
		    model.addAttribute("edit", category);
			return "editCategory";
		}else {
		    model.addAttribute("error", "你要修改的信息不存在");
			return "error";
		}
	}
	// 修改类别
	@RequestMapping(value="/category/{id}",method=RequestMethod.POST)
	public String editData(ModelMap model,Category category) {
		Integer result = categoryService.update(category);
		if (result.equals(1)) return "redirect:/category?start="+nowStart(categoryService.total(category.getId()));
	    model.addAttribute("error", "修改类别出错");
		return "error";
	}

	
}